
// var host = "http://192.168.0.105/";
var host = 'http://localhost:8080/GongjiaoDemo/MyServlet?url=http://221.131.92.159:8090/';
$(document).ready(function(){
	$('#back').on('click', function() {
		if($.isIOS()){
			var url = "bus.close";
			document.location = url;
		} else {
			try{
				bus.close();
			}catch(e){
				$.alert('bus.close()不存在！');
			}
		}
	});
});

(function($) {
  'use strict';
  
	
	$.list= function(url, params,success){
		$.AMUI.progress.start();
		
		url = host+url;
		$.ajax({
			url : url,
			type : 'post',
			dataType : 'json',
			data : params,
			success :success ,
			error : function(){
				$.AMUI.progress.done();
				var error = $('.list-error');
				if(error.length == 0){
					var html = '<div class="list-error"><span class="list-error-content">加载失败</span></div>';
					$('body').append(html);
					error = $('.list-error');
					setTimeout(function(){
						error.hide();
					},2000);
				}else{
					error.show();
					setTimeout(function(){
						error.hide();
					},2000);
				}
			}
		});
	},
	
	$.request = function(url, params,success,error){
		
		url = host+url;

		$.ajax({
			url : url,
			type : 'post',
			dataType : 'json',
			data : params,
			success :success ,
			error : error
		});
	},
	
	$.asyncrequest = function(url, params,success,error){
		url = host+url;
		$.ajax({
			url : url,
			type : 'post',
			dataType : 'json',
			async:false,
			data : params,
			success :success ,
			error : error
		});
	},
	
	$.query = function(url,params,success,error){
		
		url = host+url;

		$.ajax({
			url : url,
			type : 'get',
			dataType : 'json',
			data : params,
			success :success ,
			error : error
		});
	},

	$.alert = function(msg){
		var error = $('.list-error');
		if(error.length == 0){
			var html = '<div class="list-error"><div class="list-error-content">'+msg+'</div></div>';
			$('body').append(html);

			setTimeout(function(){
				$('.list-error').hide();
			},2000);
		}else{
			error.find('.list-error-content').html(msg);
			error.show();
			setTimeout(function(){
				error.hide();
			},2000);
		}
	},
	
	$.getLocationQueryData = function(){
		var query = {};
		var q = location.search.substr(1);   
		var qs = q.split("&");   
		if (qs) {   
			for (var i=0;i<qs.length;i++) {
				var key  = qs[i].substring(0,qs[i].indexOf("="));
				var val = qs[i].substring(qs[i].indexOf("=")+1);
				query[key] = val;   
			}   
		}
		return query;
	},
	
	$.getLocationQuery = function(key){
		var value = '';
		var q = location.search.substr(1);   
		var qs = q.split("&");   
		if (qs) {   
			for (var i=0;i<qs.length;i++) {
				var key  = qs[i].substring(0,qs[i].indexOf("="));
				var val = qs[i].substring(qs[i].indexOf("=")+1);
				value = val;   
				break;
			}   
		}
		return value;
	},
	
	$.isAndroid = function(){
		var u = navigator.userAgent;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
		return isAndroid;
	},
	
	$.isIOS = function(){
		var u = navigator.userAgent;
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
		return isiOS;
	}
	

})(jQuery);

//定义一些常量
var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
var PI = 3.1415926535897932384626;
var a = 6378245.0;
var ee = 0.00669342162296594323;

function transferBaidu2WGS84(bd_lon, bd_lat) {
	var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    var x = bd_lon - 0.0065;
    var y = bd_lat - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    var lng = z * Math.cos(theta);
    var lat = z * Math.sin(theta);
    
    if (out_of_china(lng, lat)) {
        return [lng, lat]
    }
    else {
        var dlat = transformlat(lng - 105.0, lat - 35.0);
        var dlng = transformlng(lng - 105.0, lat - 35.0);
        var radlat = lat / 180.0 * PI;
        var magic = Math.sin(radlat);
        magic = 1 - ee * magic * magic;
        var sqrtmagic = Math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
        mglat = lat + dlat;
        mglng = lng + dlng;
        return [lng * 2 - mglng, lat * 2 - mglat]
    }
}

function transformlat(lng, lat) {
    var ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
    ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
    return ret
}

function transformlng(lng, lat) {
    var ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
    ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
    return ret
}

/**
 * 判断是否在国内，不在国内则不做偏移
 * @param lng
 * @param lat
 * @returns {boolean}
 */
function out_of_china(lng, lat) {
    return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
}

Date.prototype.Format = function(fmt)   
{ //author: meizz   
  var o = {   
    "M+" : this.getMonth()+1,                 //月份   
    "d+" : this.getDate(),                    //日   
    "h+" : this.getHours(),                   //小时   
    "m+" : this.getMinutes(),                 //分   
    "s+" : this.getSeconds(),                 //秒   
    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
    "S"  : this.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}  

